package net.cnki.sunny.service.impl;

import cn.hutool.core.exceptions.ValidateException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.cnki.sunny.base.BaseService;
import net.cnki.sunny.model.dto.RoleDTO;
import net.cnki.sunny.model.dto.UserDTO;
import net.cnki.sunny.model.entity.Role;
import net.cnki.sunny.model.entity.User;
import net.cnki.sunny.model.mapper.RoleMapper;
import net.cnki.sunny.model.vo.RoleVO;
import net.cnki.sunny.service.RoleService;
import net.cnki.sunny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends BaseService<Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;
    @Autowired
    private UserService userService;

    @Override
    public void addRole(Role role) {
        //名称不能重复
        Role condition = new Role();
        condition.setRoleName(role.getRoleName());
        Role roleExist = selectOne(condition);
        if (ObjectUtils.isEmpty(roleExist)) {
            role.setCreateTime(new Date(System.currentTimeMillis()));
            insertSelective(role);
        } else {
            throw new ValidateException("角色已存在");
        }
    }

    @Override
    public void modifyRole(Role role) {
        //角色存在
        Role oldRole = selectByPrimaryKey(role.getId());
        if (ObjectUtils.isEmpty(oldRole)) {
            throw new ValidateException("角色不存在");
        }
        //名称不能重复
        if (StringUtils.hasText(role.getRoleName())) {
            Role condition = new Role();
            condition.setRoleName(role.getRoleName());
            List<Role> roleList = select(condition);
            if ((oldRole.getRoleName().equals(role.getRoleName()) && roleList.size() > 1)
                    || (!oldRole.getRoleName().equals(role.getRoleName()) && roleList.size() > 0)) {
                throw new ValidateException("角色已存在");
            }
        }
        role.setUpdateTime(new Date(System.currentTimeMillis()));
        updateByPrimaryKeySelective(role);
    }


    @Override
    public List<User> getNotAssignedRoleUser(Integer id, String username) {
        UserDTO userDTO = new UserDTO();
        userDTO.setUsername(username);
        List<User> userList = userService.selectByCondition(userDTO);
        List<Integer> userIdList = roleMapper.selectAssignedUser(id);
        for (Integer userId : userIdList) {
            userList = userList.stream().filter(user -> !user.getId().equals(userId)).collect(Collectors.toList());
        }
        return userList;
    }

    @Override
    @Transactional
    public void modifyRoleUser(RoleDTO roleDTO) {
        //删除已有
        if (!CollectionUtils.isEmpty(roleDTO.getUserIds())) {
            roleMapper.deleteUserRole(roleDTO.getId());
        }
        //添加新增
        roleMapper.batchAddUserRole(roleDTO.getUserIds(), roleDTO.getId());
    }

    @Override
    @Transactional
    public void deleteRole(Integer id) {
        roleMapper.deleteUserRole(id);
        deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<RoleVO> getRoleList(RoleDTO roleDTO) {
        PageHelper.startPage(roleDTO.getPageNum(), roleDTO.getPageSize());
        return new PageInfo<RoleVO>(roleMapper.selectRoleUserList());
    }
}
